using System;
using UnityEngine;
using QFramework;
using Unity.VisualScripting;

namespace QFramework.Monk
{
	public partial class SimpleAxe : AbilityBase
	{
		public override void InitAbility()
		{
			mCount = 2;
			//mLevel = 0;
			//base.ShowAbility();
		}

		private void Update()
		{
			if (!AppStart.CheckGameState(GameState.GamePlaying))
			{
				return;
			}
			mCurTime+=Time.deltaTime;
			if (mCurTime > AppStart.ackDuration.Value)
			{
				for (int i = 0; i < mCount; i++)
				{
					CreateAxe();
				}
				mCurTime=0;
			}
		}

		private void CreateAxe()
		{
			Axe.InstantiateWithParent(transform).Show().Position(transform.position)
				.Self(self =>
				{
					var randomX = RandomUtility.Choose(-8f,-3, 8f, 3);
					var  randomY = RandomUtility.Choose( 3f, 5f,8f);
					var selfCache = self;
					selfCache.velocity = new Vector2(randomX, randomY);
						
					selfCache.OnTriggerEnter2DEvent(collider2D =>
					{
						if (collider2D.gameObject.layer == LayerMask.NameToLayer("Enemy"))
						{
							var enemy = collider2D.GetComponentInParent<EnemyBase>();
							if (enemy!=null)
							{
								//是否可击退
								if (mIsSuper)
								{
									var dir = (enemy.Position()-this.Position()).normalized;
									enemy.HitBack(dir*mHitBackFactor);
									
								}
								enemy.CostHp(AppStart.damage.Value);
							}
						}
				
					}).UnRegisterWhenGameObjectDestroyed(selfCache.gameObject);

					ActionKit.OnUpdate.Register(() =>
					{
						if (Player.Default!= null)
						{
							if (Player.Default.Position().y-self.Position().y>10)
							{
								self.gameObject.DestroySelfGracefully();
							}
							else
							{
								//旋转自己
								self.transform.Rotate(Vector3.forward,Time.deltaTime*360);
							}
						}
						
							
					}).UnRegisterWhenGameObjectDestroyed(self.gameObject);

				});
		}
	}
}
